﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Diagnostics;
using System.IO;
using AnalysisLib;
using ElevatorLib;

namespace ElevatorSIM
{
    public partial class MainForm : KryptonForm
    {
        private string configPath = Application.StartupPath + "\\sim.conf";
        private string initPath = Application.StartupPath + "\\Sys.ini";
        private IniFile ini =null;
        private Button ShowBuildingHeightsButton = new Button();
        private SimulationEngine se =null;
        private Boolean valueChanged = false;
        private Language language = null;
       
        public MainForm()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            
        }

        private void init()
        {
            Debug.Listeners.Add(new ConsoleTextBoxListener(this.Console_TextBox));
            Debug.AutoFlush = true;

            ini = new IniFile(initPath);
            Parameter_DataGridView.Rows.Add(8);
            se = new SimulationEngine(configPath, ini.GetString("PersonData", "Path", "PassengerCreator/Person.dat"));
            Observer ob = new Observer(this);
            SimulationEngine.Login(ob);
            se.Init();
            ChangeLanguage(ini.GetString("Language", "Type", "English"), this);
            this.System_Information_TextBox.AppendText(language.Passenger_Data_Name + "：" + "\n" + SimulationEngine.PersonCreator.Name + "\n");
            this.System_Information_TextBox.AppendText(language.Author + "：" + SimulationEngine.PersonCreator.Author + "\n");
            this.System_Information_TextBox.AppendText(language.Description + "：" + "\n" + SimulationEngine.PersonCreator.Description + "\n");
            this.System_Information_TextBox.AppendText("------------------------------------------------------\n");
            this.System_Information_TextBox.AppendText(language.Strategy_Name + "：" + "\n" + SimulationEngine.ControlStrategy.Name + "\n");
            this.System_Information_TextBox.AppendText(language.Author + "：" + SimulationEngine.ControlStrategy.Author + "\n");
            this.System_Information_TextBox.AppendText(language.Description + "：" + "\n" + SimulationEngine.ControlStrategy.Description + "\n");
        }

        #region 国际化
        public void ChangeLanguage(string Language, MainForm form)
        {
            language = new Language(Language);

            form.FileToolStripMenuItem.Text = language.File;
            form.SettingsToolStripMenuItem.Text = language.Settings;
            form.RunToolStripMenuItem.Text = language.Run;
            form.HelpToolStripMenuItem.Text = language.Help;
            form.NewToolStripMenuItem.Text = language.New;
            form.Group_Control_Strategy_ProjectToolStripMenuItem.Text = language.Group_Control_Strategy_Project;
            form.Elevator_Behavior_ProjectToolStripMenuItem.Text = language.Elevator_Behavior_Project;
            form.Passenger_Traffic_Generation_ProjectToolStripMenuItem.Text = language.Passenger_Traffic_Generation_Project;
            form.Analysis_Module_ProjectToolStripMenuItem.Text = language.Analysis_Module_Project;
            form.OpenToolStripMenuItem.Text = language.Open;
            form.Group_Control_Strategy_ProjectToolStripMenuItem1.Text = language.Group_Control_Strategy_Project;
            form.Elevator_Behavior_ProjectToolStripMenuItem1.Text = language.Elevator_Behavior_Project;
            form.Passenger_Traffic_Generation_ProjectToolStripMenuItem1.Text = language.Passenger_Traffic_Generation_Project;
            form.Analysis_Module_ProjectToolStripMenuItem1.Text = language.Analysis_Module_Project;
            form.ExitToolStripMenuItem.Text = language.Exit;
            form.Passenger_Flow_FileToolStripMenuItem.Text = language.Passenger_Flow_File;
            form.Control_StrategyToolStripMenuItem.Text = language.Control_Strategy;
            form.Elevator_BehaviorToolStripMenuItem.Text = language.Elevator_Behavior;
            form.Analysis_ModuleToolStripMenuItem.Text = language.Analysis_Module;
            form.System_SettingsToolStripMenuItem.Text = language.System_Settings;
            form.StartToolStripMenuItem.Text = language.Start;
            form.SuspendToolStripMenuItem.Text = language.Suspend;
            form.StopToolStripMenuItem.Text = language.Stop;
            form.About_ElevatorSIMToolStripMenuItem.Text = language.About_ElevatorSIM;
            form.ElevatorSIM_ManualToolStripMenuItem.Text = language.ElevatorSIM_Manual;
            form.ElevatorLib_ManualToolStripMenuItem.Text = language.ElevatorLib_Manual;
            form.Getting_StartedToolStripMenuItem.Text = language.Getting_Started;
            form.Single_StepToolStripMenuItem.Text = language.Single_Step;


            form.System_Header.Text = language.System_Header;
            form.Parameter_Header.Text = language.Parameter_Header;
            Parameter_DataGridView[0, 0].Value = language.FloorNum;
            Parameter_DataGridView[1, 0].Value = Building.ElevatorNum;
            Parameter_DataGridView[0, 1].Value = language.FloorHeight;
            ShowBuildingHeightsButton.Visible = false;
            Parameter_DataGridView.Controls.Add(ShowBuildingHeightsButton);
            ShowBuildingHeightsButton.MouseClick += ShowBuildingHeightsButton_Click;
            Parameter_DataGridView[0, 2].Value = language.ElevatorNum;
            Parameter_DataGridView[1, 2].Value = Building.Elevators.Count;
            Parameter_DataGridView[0, 3].Value = language.Container;
            Parameter_DataGridView[1, 3].Value = Building.Elevators[0].Container;
            Parameter_DataGridView[0, 4].Value = language.Velocity;
            Parameter_DataGridView[1, 4].Value = Building.Elevators[0].RunningBehavior.NormalSpeed;
            Parameter_DataGridView[0, 5].Value = language.OpenCloseTime;
            Parameter_DataGridView[1, 5].Value = Building.Elevators[0].OpenCloseTime;
            Parameter_DataGridView[0, 6].Value = language.RunningBehavior;
            Parameter_DataGridView[1, 6].ReadOnly = true;

            Parameter_DataGridView[1, 6].Value = Building.Elevators[0].RunningBehavior;

            Parameter_DataGridView[0, 7].Value = language.PersonCreator;
            Parameter_DataGridView[1, 7].Value = SimulationEngine.PersonCreator.ToString();
            Parameter_DataGridView[1, 7].ReadOnly = true;

            Parameter_DataGridView[0, 8].Value = language.Strategy;
            Parameter_DataGridView[1, 8].Value = SimulationEngine.ControlStrategy.ToString();
            Parameter_DataGridView[1, 8].ReadOnly = true;
            valueChanged = true;

        }
        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.init();
        }

        private void Center_SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
    
            tabControl1.SelectedTab.Invalidate();
        }

        private void System_SettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SystemSetting sys = new SystemSetting(this);
            sys.ShowDialog();
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Start_Button_Click(object sender, EventArgs e)
        {
            this.SimulationEngineRun();
        }

        private void SimulationEngineRun()
        {
            if (se.IsPause ||se.IsStep)
                se.GoOn();
            else
            {
                Console_TextBox.Text = "";
                se.Init();
                se.Start();
                Console_TextBox.Focus();
            }

            
        }

        private void Stop_Button_Click(object sender, EventArgs e)
        {
            this.SimulationEngineStop();          
        }

        private void SimulationEngineStop()
        {
            se.Stop();
            
        }

        private void Suspend_Button_Click(object sender, EventArgs e)
        {
            this.SimulationEnginePause();
        }
        private void SimulationEnginePause()
        {
            se.Pause();
        }

        private void Single_Step_Button4_Click(object sender, EventArgs e)
        {
            this.SimulationEngineSingleStep();
            
        }

        private void SimulationEngineSingleStep()
        {
            if (se.IsStep)
            {
                se.SetStep(false);
            }
            else se.SetStep(true);
            
        }

        private void Parameter_DataGridView_Click(object sender, EventArgs e)
        {
            if (Parameter_DataGridView.CurrentCell.RowIndex == 1 && Parameter_DataGridView.CurrentCell.ColumnIndex==1)
            {
                ShowBuildingHeightsButton.Text = "...";
                Rectangle R = Parameter_DataGridView.GetCellDisplayRectangle(Parameter_DataGridView.CurrentCell.ColumnIndex, Parameter_DataGridView.CurrentCell.RowIndex, false);  //获取单元格位置
                ShowBuildingHeightsButton.SetBounds(R.X, R.Y, R.Width, R.Height); 
                ShowBuildingHeightsButton.Visible = true;
            }
            else
                ShowBuildingHeightsButton.Visible = false;
        }

        private void ShowBuildingHeightsButton_Click(object sender, EventArgs e)
        {
            SetBuildingHeight buildingHeight = new SetBuildingHeight( se);
            buildingHeight.ShowDialog();
        }

        private void Parameter_DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SimulationConfig conf = new SimulationConfig(configPath);
            if (valueChanged && Parameter_DataGridView.CurrentCell.ColumnIndex == 1)
            {
                if (Parameter_DataGridView.CurrentCell.RowIndex == 0)
                    conf.UpdateFloorNum(Convert.ToInt32(Parameter_DataGridView[1, 0].Value.ToString()));
                if (Parameter_DataGridView.CurrentCell.RowIndex == 2)
                    conf.UpdateElevatorNum(Convert.ToInt32(Parameter_DataGridView[1, 2].Value.ToString()));
                if (Parameter_DataGridView.CurrentCell.RowIndex == 3)
                    conf.UpdateContainer(Convert.ToInt32(Parameter_DataGridView[1, 3].Value.ToString()));
                if (Parameter_DataGridView.CurrentCell.RowIndex == 4)
                    conf.UpdateVelocity(Convert.ToDouble(Parameter_DataGridView[1, 4].Value.ToString()));
                if (Parameter_DataGridView.CurrentCell.RowIndex == 5)
                    conf.UpdateOpenClose(Convert.ToDouble(Parameter_DataGridView[1, 5].Value.ToString()));
                if (Parameter_DataGridView.CurrentCell.RowIndex == 9)
                    conf.UpdateAnalysis(Parameter_DataGridView[1, 9].Value.ToString());

                se.Init();
                this.System_Information_TextBox.AppendText(language.Passenger_Data_Name + "：" + "\n" + SimulationEngine.PersonCreator.Name + "\n");
                this.System_Information_TextBox.AppendText(language.Author + "：" + SimulationEngine.PersonCreator.Author + "\n");
                this.System_Information_TextBox.AppendText(language.Description + "：" + "\n" + SimulationEngine.PersonCreator.Description + "\n");
                this.System_Information_TextBox.AppendText("------------------------------------------------------\n");
                this.System_Information_TextBox.AppendText(language.Strategy_Name + "：" + "\n" + SimulationEngine.ControlStrategy.Name + "\n");
                this.System_Information_TextBox.AppendText(language.Author + "：" + SimulationEngine.ControlStrategy.Author + "\n");
                this.System_Information_TextBox.AppendText(language.Description + "：" + "\n" + SimulationEngine.ControlStrategy.Description + "\n");
            }
           
        }

        private void Passenger_Flow_FileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(OpenFileDialog.ShowDialog()==DialogResult.OK)
            {
                se.Path = OpenFileDialog.FileName;
                if (se.Init())
                {
                    ini.WriteValue("PersonData", "Path", se.Path);
                }
                else
                {
                    Language language = new Language(ini.GetString("Language", "Type", "English"));
                    MessageBox.Show(language.ReadPersonDatError);
                }
                
                
            }
        }

        private void StartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SimulationEngineRun();
        }

        private void SuspendToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SimulationEnginePause();
        }

        private void StopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SimulationEngineStop();
        }

        private void Single_StepToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SimulationEngineSingleStep();
        }

    }
}